package com.yuanduan.mplus.view;

import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cj.framework.app.BaseActivity;
import com.cj.framework.app.BaseApplication;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.cj.framework.util.IOkHttp;
import com.cj.framework.util.Msg;
import com.cj.framework.util.OkHttpImp;
import com.cj.framework.util.RSAUtils;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.db.dao.UserDao;
import com.yuanduan.mplus.db.entity.User;
import com.yuanduan.mplus.model.entity.RS;

import java.util.HashMap;
import java.util.List;

/**
 * 反馈界面
 */
public class FeedbackActivity extends BaseActivity implements View.OnClickListener {
    private Toolbar toolbar;
    private EditText et_message;
    private Button btn_submit;
    private IOkHttp okHttp=new OkHttpImp();
    private IOkHttp.IHttpCallback callback=new IOkHttp.IHttpCallback() {
        @Override
        public void onResponse(Msg msg, int index) {
            String message=msg.content();
            RS rs= (RS) Config.decry(message,RS.class);
            if(rs.getRs()==1){
                Toast.makeText(FeedbackActivity.this,"提交成功",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(FeedbackActivity.this,"提交失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onError(Exception e) {
            Toast.makeText(FeedbackActivity.this,"网络出错",Toast.LENGTH_SHORT).show();
        }
    };
//    private IHttp.HttpInfoCallback httpInfoCallback=new IHttp.HttpInfoCallback() {
//        @Override
//        public void onSucceed(Object result) {
//            String str= RSAUtils.decryptWithRSA((String) result);
//            RS rs= (RS) Config.decry(result,RS.class);
//            if(rs.getRs()==1){
//                Toast.makeText(FeedbackActivity.this,"提交成功",Toast.LENGTH_SHORT).show();
//            }else{
//                Toast.makeText(FeedbackActivity.this,"提交失败",Toast.LENGTH_SHORT).show();
//            }
//        }
//
//        @Override
//        public void onFailed(Exception e) {
//            Toast.makeText(FeedbackActivity.this,"提交失败",Toast.LENGTH_SHORT).show();
//        }
//    };

    @Override
    protected void findId() {
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        et_message= (EditText) findViewById(R.id.feedback_et_suggestion);
        btn_submit= (Button) findViewById(R.id.feedback_btn_submit);
    }

    @Override
    protected void logic() {
        toolbar.setTitle("意见反馈");
        toolbar.setNavigationIcon(R.mipmap.back_arrow_black);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FeedbackActivity.this.finish();
            }
        });
        btn_submit.setOnClickListener(this);

        et_message.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if(s.length()>0){
                    btn_submit.setBackgroundResource(R.drawable.btn_login1);
                }else{
                    btn_submit.setBackgroundResource(R.drawable.feedback_btn);
                }
            }
        });
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_feedback;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.feedback_btn_submit:
                String str=et_message.getText().toString().trim();
                if(TextUtils.isEmpty(str)){
                    return;
                }
                HashMap<String,String> hashMap=new HashMap<>();
                UserDao userDao= BaseApplication.getDaoSession().getUserDao();
                List list= userDao.queryBuilder().list();
                User user = null;
                if(list.size()!=0){
                    user= (User) list.get(0);
                }else{
                    return;
                }
                hashMap.put("UserPhone",user.getUserPhone());
                hashMap.put("UserPassword",user.getUserPwd());
                hashMap.put("FeedbackText",str);
                okHttp.httpInfoPost(Config.URL+Config.FEEDBACK,Config.translateMap(hashMap),callback);
                break;
        }
    }
}
